package com.gitee.clarence.swagger.utils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

import com.thoughtworks.qdox.library.SortedClassLibraryBuilder;
import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.parser.ParseException;

/**
 * Created by IntelliJ IDEA. User: luopeng Date: 2017-03-24 下午4:33
 */
public class SourceReader {
	private static final Logger LOGGER = LoggerFactory.getLogger(SourceReader.class);

	private SourceReader() {
	}

	public static List<JavaClass> readFile(File file) throws IOException {
		SortedClassLibraryBuilder classLibraryBuilder = new SortedClassLibraryBuilder();
		classLibraryBuilder.appendDefaultClassLoaders();
		JarFile jarFile = new JarFile(file);
		try {
			List<JarEntry> entryList = Collections.list(jarFile.entries()).stream()
					.filter(entry -> !entry.isDirectory() && entry.getName().endsWith(".java"))
					.collect(Collectors.toList());
			for (JarEntry jarEntry : entryList) {
				try (InputStream in = jarFile.getInputStream(jarEntry)) {
					classLibraryBuilder.addSource(in);
				}
			}
		} catch (Exception e) {
			LOGGER.warn("", e);
		} finally {
			jarFile.close();
		}

		return new ArrayList<>(classLibraryBuilder.getClassLibrary().getJavaClasses());
	}

	public static List<JavaClass> readJavaFiles(List<File> classList) throws IOException {
		List<JavaClass> result = new ArrayList<>();
		if (CollectionUtils.isEmpty(classList))
			return result;
		SortedClassLibraryBuilder classLibraryBuilder = new SortedClassLibraryBuilder();
		classLibraryBuilder.appendDefaultClassLoaders();
		for (File file : classList) {
			if (StringUtils.endsWith(file.getPath(), "Controller.java")) {
				try {
					classLibraryBuilder.addSource(file);
				} catch (ParseException e) {
					//LOGGER.info("", e);
				}
				
			}
		}
		return new ArrayList<>(classLibraryBuilder.getClassLibrary().getJavaClasses());

	}

	/**
	 * 把class转换成map
	 * 
	 * @param classList
	 * @return
	 */
	public static Map<String, JavaClass> transforJavaClass(List<JavaClass> classList) {
		Map<String, JavaClass> javaClassMap = new HashMap<>();
		for (JavaClass javaClass : classList) {
			javaClassMap.put(javaClass.getFullyQualifiedName(), javaClass);
		}
		return javaClassMap;
	}
}
